package com.first.agent.service.impl;

import com.first.agent.common.JsonResult;
import com.first.agent.common.R;
import com.first.agent.dao.impl.CustomerDaoImp;
import com.first.agent.pojo.AgentCustomer;
import com.first.agent.pojo.Contacts;
import com.first.agent.pojo.Keyword;
import com.first.agent.service.CustomerService;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lxm
 * Date: 2022/11/15 9:59
 * Description:
 * Version: V1.0
 */
public class CustomerServiceImp implements CustomerService {
   CustomerDaoImp cdi=new CustomerDaoImp();
    @Override
    public JsonResult customerTable(int page, int limit,String customerName) {
        JsonResult jsonResult=new JsonResult();
        //调用dao，存入JsonResult
        try {
            jsonResult.setCode(0);
            jsonResult.setMsg("查询成功");
            List list = cdi.customerQuery((page - 1) * limit, limit,customerName);
            int count = cdi.countCustomer(customerName);
            jsonResult.setData(list);
            jsonResult.setCount(count);
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统错误");
        }
        return jsonResult;
    }

    @Override
    public R addCustomer(AgentCustomer customer, Contacts contacts) {
        R r=new R();
        //调用dao进行新增
        try {
            int count = cdi.countCustomer(customer.getCompany_name());
            if(count>0){
                r.setCode(500);
                r.setMsg("客户已存在");
            }else{
                try {
                    int i = cdi.addCustomer(customer, contacts);
                    if (i>0){
                        r.setCode(200);
                        r.setMsg("添加成功");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    r.setCode(500);
                    r.setMsg("添加失败");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统错误");
        }
        return r;
    }

    @Override
    public R delCustomer(String cus_id,String state) {
        R r=new R();
        //调用dao进行停用
        if(state.equals("2")){
            r.setCode(500);
            r.setMsg("已停用，无需再次停用");
        }else {
            try {
                int result = cdi.delCustomer(Integer.parseInt(cus_id));
                if (result > 0) {
                    r.setCode(200);
                    r.setMsg("停用成功");
                } else {
                    r.setCode(500);
                    r.setMsg("请求错误");
                }

            } catch (SQLException e) {
                e.printStackTrace();
                r.setCode(500);
                r.setMsg("请求错误");
            }
        }

        return r;
    }

    @Override
    public R openCustomer(String cus_id,String state) {
        R r=new R();
        //调用dao进行启用
        //调用dao进行停用
        if(state.equals("1")){
            r.setCode(500);
            r.setMsg("已启用，无需再次启用");
        }else {
            try {
                int result = cdi.openCustomer(Integer.parseInt(cus_id));
                if (result > 0) {
                    r.setCode(200);
                    r.setMsg("启用成功");
                } else {
                    r.setCode(500);
                    r.setMsg("请求错误");
                }

            } catch (SQLException e) {
                e.printStackTrace();
                r.setCode(500);
                r.setMsg("请求错误");
            }
        }
        return r;
    }

    @Override
    public R updateCustomer(AgentCustomer customer, Contacts contacts) {
        R r=new R();
        //调用dao进行更新
                try {
                    int i = cdi.updateCustomer(customer, contacts);
                    if (i>0){
                        r.setCode(200);
                        r.setMsg("更新成功");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    r.setCode(500);
                    r.setMsg("系统错误，更新失败");

            }


        return r;
    }

    @Override
    public R addKeyWords(Keyword keyword) {
        R r=new R();
        //设置申请起止时间
        Date date=new Date();
        keyword.setApplication_begin(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR,1);
        keyword.setApplication_end(cal.getTime());


        //调用dao进行存储



        try {
            int i = cdi.keyWordCustomer(keyword.getCustomer_name());
            if (i>0) {


                int result = cdi.addKeyWords(keyword);
                if (result > 0) {
                    r.setCode(200);
                    r.setMsg("提交申请成功");
                } else {
                    r.setMsg("关键词已存在，请重新输入");
                    r.setCode(500);
                }
            }else{
                r.setCode(500);
                r.setMsg("客户不存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统错误，申请失败");
        }


        return r;
    }

    @Override
    public R keyWordCustomerSearch(String customer) {
        R r=new R();
        //调用dao进行查询
        try {
            int customernum = cdi.keyWordCustomer(customer);
            System.out.println(customernum);
            if(customernum>0){
                r.setCode(200);
                r.setMsg("客户存在");
            }else{
                r.setCode(500);
                r.setMsg("客户不存在，重新输入");
            }

        } catch (SQLException e) {
            e.printStackTrace();

        }

        return r;
    }
}
